package com.lxp.config;

import com.lxp.common.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author lxp
 * @Motto 每天一行小代码，健康幸福学JAVA：
 * @create 2022-08-23 23:04
 * @Description:
 */

@Configuration
@Slf4j
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * TODO：或者在资源路径下创建static然后进行yml配置也可
     * 静态资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始进行资源映射");
        log.info("开始进行静态资源映射...");
        registry.addResourceHandler("/static/backend/**").addResourceLocations("classpath:/static/backend/");
        registry.addResourceHandler("/static/front/**").addResourceLocations("classpath:/static/front/");
    }

    /**
     * 扩展mvc框架的消息转换器
     * @param converters
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {

        // 创建消息转换器对象
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        // 设置对象转换器，底层使用Jackson将Java对象转换为Json
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        // 将上面的消息中器对象追加到mvc框架的转换器集合中
        converters.add(0,messageConverter);
    }
}
